package com.wc.alorithm_blue_bridge._模拟.Q7938;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/12/19-22:16
 */
public class Main {
    static char[][] map = new char[30][];
    static int[] positionX = new int[]{1, 0, -1, 0};
    static int[] positionY = new int[]{0, 1, 0, -1};

    public static void main(String[] args) {
        String s = "0000100010000001101010101001001100000011\n" +
                "0101111001111101110111100000101010011111\n" +
                "1000010000011101010110000000001011010100\n" +
                "0110101010110000000101100100000101001001\n" +
                "0000011010100000111111001101100010101001\n" +
                "0110000110000000110100000000010010100011\n" +
                "0100110010000110000000100010000101110000\n" +
                "0010011010100110001111001101100110100010\n" +
                "1111000111101000001110010001001011101101\n" +
                "0011110100011000000001101001101110100001\n" +
                "0000000101011000010011111001010011011100\n" +
                "0000100000011001000100101000111011101100\n" +
                "0010110000001000001010100011000010100011\n" +
                "0110110000100011011010011010001101011011\n" +
                "0000100100000001010000101100000000000010\n" +
                "0011001000001000000010011001100101000110\n" +
                "1110101000011000000100011001001100111010\n" +
                "0000100100111000001101001000001010010001\n" +
                "0100010010000110100001100000110111110101\n" +
                "1000001001100010011001111101011001110001\n" +
                "0000000010100101000000111100110010101101\n" +
                "0010110101001100000100000010000010110011\n" +
                "0000011101001001000111011000100111010100\n" +
                "0010001100100000011000101011000000010101\n" +
                "1001111010010110011010101110000000101110\n" +
                "0110011101000010100001000101001001100010\n" +
                "1101000000010010011001000100110010000101\n" +
                "1001100010100010000100000101111111111100\n" +
                "1001011010101100001000000011000110110000\n" +
                "0011000100011000010111101000101110110001";
        String[] ss = s.split("\\n");
        for (int i = 0; i < 30; i++) {
            map[i] = ss[i].toCharArray();
        }
        dfs(0,0);
        int ans = 0;
        for (int i = 0; i < 30; i++) {
            for (int j = 0; j < 40; j++) {
                if (map[i][j] == '2') {
                    ans++;
                }
            }
        }
        System.out.println(ans);
    }


    public static void dfs(int x, int y) {
        if (isBorder(x, y)) {
            return;
        }

        if (map[x][y] == '0') {
            map[x][y] = '2';
            for (int i = 0; i < 4; i++) {
                dfs(x + positionX[i], y + positionY[i]);
            }
        }
    }

    public static boolean isBorder(int x, int y) {
        return x < 0 || x == 30 || y < 0 || y == 40;
    }
}
